package com.yy.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.yy.common.annotation.Log;
import com.yy.common.core.controller.BaseController;
import com.yy.common.core.domain.AjaxResult;
import com.yy.common.core.page.TableDataInfo;
import com.yy.common.enums.BusinessType;
import com.yy.common.utils.poi.ExcelUtil;
import com.yy.common.constant.PmsConstants;
import com.yy.domain.PayFeeItem;
import com.yy.service.IPayFeeItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 收费项管理Controller
 * 
 * @author yy
 * @date 2023-10-26
 */
@RestController
@RequestMapping("/pay/feeItem")
public class PayFeeItemController extends BaseController
{
    @Autowired
    private IPayFeeItemService payFeeItemService;

    /**
     * 查询收费项管理列表
     */
    @PreAuthorize("@ss.hasPermi('pay:feeItem:list')")
    @GetMapping("/list")
    public TableDataInfo list(PayFeeItem payFeeItem)
    {
        startPage();
        List<PayFeeItem> list = payFeeItemService.selectPayFeeItemList(payFeeItem);
        return getDataTable(list);
    }

    /**
     * 导出收费项管理列表
     */
    @PreAuthorize("@ss.hasPermi('pay:feeItem:export')")
    @Log(title = "收费项管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PayFeeItem payFeeItem)
    {
        List<PayFeeItem> list = payFeeItemService.selectPayFeeItemList(payFeeItem);
        ExcelUtil<PayFeeItem> util = new ExcelUtil<PayFeeItem>(PayFeeItem.class);
        util.exportExcel(response, list, "收费项管理数据");
    }

    /**
     * 获取收费项管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('pay:feeItem:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(payFeeItemService.selectPayFeeItemById(id));
    }

    /**
     * 新增收费项管理
     */
    @PreAuthorize("@ss.hasPermi('pay:feeItem:add')")
    @Log(title = "收费项管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PayFeeItem payFeeItem)
    {
        return toAjax(payFeeItemService.insertPayFeeItem(payFeeItem));
    }

    /**
     * 修改收费项管理
     */
    @PreAuthorize("@ss.hasPermi('pay:feeItem:edit')")
    @Log(title = "收费项管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PayFeeItem payFeeItem)
    {
        return toAjax(payFeeItemService.updatePayFeeItem(payFeeItem));
    }

    /**
     * 删除收费项管理
     */
    @PreAuthorize("@ss.hasPermi('pay:feeItem:remove')")
    @Log(title = "收费项管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(payFeeItemService.deletePayFeeItemByIds(ids));
    }

    /**
     * 获常规收费项目
     */
    @GetMapping("getHousePayFeeItem")
    public AjaxResult getHousePayFeeItem(){
        PayFeeItem payFeeItem = new PayFeeItem();
        payFeeItem.setType(PmsConstants.PAY_FEE_ITEM_TYPE_HOUSE);
        List<PayFeeItem> feeItems = this.payFeeItemService.selectPayFeeItemList(payFeeItem);
        return AjaxResult.success(feeItems);
    }

    /**
     * 获取押金类收费项目
     */
    @GetMapping("getDepositPayFeeItem")
    public AjaxResult getDepositPayFeeItem(){
        PayFeeItem payFeeItem = new PayFeeItem();
        payFeeItem.setType(PmsConstants.PAY_FEE_ITEM_TYPE_DEPOSIT);
        List<PayFeeItem> feeItems = this.payFeeItemService.selectPayFeeItemList(payFeeItem);
        return AjaxResult.success(feeItems);
    }

    /**
     * 获取临时类收费项目
     */
    @GetMapping("getTempPayFeeItem")
    public AjaxResult getTempPayFeeItem(){
        PayFeeItem payFeeItem = new PayFeeItem();
        payFeeItem.setType(PmsConstants.PAY_FEE_ITEM_TYPE_TEMP);
        List<PayFeeItem> feeItems = this.payFeeItemService.selectPayFeeItemList(payFeeItem);
        return AjaxResult.success(feeItems);
    }



    /**
     * 根据类型查询已配置默认常规收费项目
     */
    @GetMapping("/getDefaultPayFeeItemByType/{type}")
    public AjaxResult getDefaultPayFeeItemByType(@PathVariable String type){
        List<PayFeeItem> payFeeItemList=this.payFeeItemService.queryDefaultPayFeeItemByType(type);
        return AjaxResult.success(payFeeItemList);
    }

    /**
     * 删除默认收费项目（根据收费项目ID和类型）
     */
    @DeleteMapping("delDefaultPayFeeItem/{id}/{type}")
    public AjaxResult delDefaultPayFeeItem(@PathVariable Long id,@PathVariable String type){
        if(ObjectUtil.isNull(id)){
            return AjaxResult.error("ID不能为空");
        }
        if(ObjectUtil.isNull(type)){
            return AjaxResult.error("类型不能为空");
        }
        return toAjax(this.payFeeItemService.deleteDefaultPayFeeItemByFeeIdAndType(id,type));

    }

    /**
     * 新增默认收费项目管理
     */
    @PostMapping("addDefaultPayItemConfig")
    public AjaxResult addDefaultPayItemConfig(String type,Long [] ids){
        if(ObjectUtil.isNull(type)){
            return AjaxResult.error("类型不能为空");
        }
        if(ids==null||ids.length==0){
            return AjaxResult.error("配置项不能为空");
        }
        return toAjax(this.payFeeItemService.addDefaultPayItemConfig(type,ids));
    }


}
